<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  data_cache.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-06-03.
//  Copyright 2011 lishuzu. All rights reserved.
// 
/**
* 数据缓存类
*/
class Datacache
{
	
	function __construct()
	{
		$this->CI =& get_instance();
		$this->CI->load->helper('cache');
	}
	//用户组更新
	function usergroup_cache(){
		$this->CI->db->order_by('gid', 'ASC');
		$query = $this->CI->db->get('usergroup');
		foreach ($query->result_array() as $row) {
			//分离每个用户组的文件 如果权限列表太长 则启用此方式
			// $usergroup = array($row['gid'] => $row);
			// cache_write('usergroup_'.$row['gid'], "config['usergroup']", $usergroup);
			$data[$row['gid']] = $row;
		}
		cache_write('usergroup', "config['usergroup']", $data);
	}
	
	//站点全局配置
	function config_cache(){
		$query = $this->CI->db->get('config');
		foreach ($query->result_array() as $key => $row) {
			$config[$row['var']] = $row['datavalue'];
		}
		cache_write('config', "config['site_config']", $config);		
	}
	
	// TAG缓存
	function tag_cache(){
		$this->CI->db->order_by('worknum', 'ASC');
		$query = $this->CI->db->get('tag');
		foreach ($query->result_array() as $row) {
			$data[$row['tagid']] = $row;
		}
		cache_write('tag', "config['tag']", $data);
	}
	
	// 统计缓存
	function statistics(){
		$data = $where = $cat_static = array();
		// 用户总数
		$data['user_num'] = $this->CI->db->count_all_results('user');
		// 作品
		$data['work_num'] = $this->CI->db->count_all_results('works');
		// 用户删除作品
		$this->CI->db->where('status', 3);
		$data['work_del_num'] = $this->CI->db->count_all_results('works');
		// 未审核作品
		$this->CI->db->where('status', 0);
		$data['work_noauth_num'] = $this->CI->db->count_all_results('works');
		// 审核作品
		$this->CI->db->where('status', 1);
		$data['work_auth_num'] = $this->CI->db->count_all_results('works');
		// 推荐作品
		$this->CI->db->where('status', 2);
		$data['work_tuijian_num'] = $this->CI->db->count_all_results('works');
		// 评论数
		$data['comment_num'] = $this->CI->db->count_all_results('comment');
		// 我说数
		$data['talk_num'] = $this->CI->db->count_all_results('talk');

		// 粉丝排行
		$this->CI->db->select('uid, username, name, fans_count, follow_count, likenum, worknum, dateline');
		$this->CI->db->order_by('fans_count', 'DESC');
		$this->CI->db->limit(20);
		$query = $this->CI->db->get('user');
		$data['fanstop'] = $query->result_array();
		// 作品排行
		$this->CI->db->select('uid, username, name, fans_count, follow_count, likenum, worknum, dateline');
		$this->CI->db->order_by('worknum', 'DESC');
		$this->CI->db->limit(20);
		$query = $this->CI->db->get('user');
		$data['worktop'] = $query->result_array();
		
		cache_write('statistics', "config['statistics']", $data);
	}
	
	// 来访缓存
	function guest_cache(){
		$this->CI->db->order_by('lastlogin', 'DESC');
		$query = $this->CI->db->get('guest');
		foreach ($query->result_array() as $row) {
			$data[$row['guestid']] = $row;
		}
		cache_write('guest', "config['guest']", $data);
	}
	// 主页缓存
	function index_cache(){
		//推荐商品
		$data = $list = array();
		$wheresql = "status = '2' ";
		$ordersql = "ORDER BY dateline DESC";
		$limit = 15;
		$query = $this->CI->db->query("SELECT * FROM ".$this->CI->db->dbprefix('works')." WHERE $wheresql $ordersql LIMIT $limit");
		foreach ($query->result_array() as $value) {
			$list[$value['wid']] = $value;
		}
		$data['recommend'] = $list;
		
		//最新商品
		$list = array();
		
		$wheresql = "status = '1' ";
		$ordersql = "ORDER BY dateline DESC";
		$limit = 15;
		$query = $this->CI->db->query("SELECT * FROM ".$this->CI->db->dbprefix('works')." WHERE $wheresql $ordersql LIMIT $limit");
		foreach ($query->result_array() as $value) {
			$list[$value['wid']] = $value;
		}
		$data['new'] = $list;
		
		// 最新热卖
		$list = array();
		$wheresql = "status != '0' AND status !='3' AND shopurl !='' ";
		$ordersql = "ORDER BY soldoutnum DESC, dateline DESC";
		$limit = 15;
		$query = $this->CI->db->query("SELECT * FROM ".$this->CI->db->dbprefix('works')." WHERE $wheresql $ordersql LIMIT $limit");

		foreach ($query->result_array() as $value) {
			$list[$value['wid']] = $value;
		}
		$data['hotsold'] = $list;
		
		// 设计师top50
		$list = array();
		$wheresql = "status = '0'";
		$ordersql = "ORDER BY fans_count DESC";
		$limit = 50;
		$query = $this->CI->db->query("SELECT * FROM ".$this->CI->db->dbprefix('user')." WHERE $wheresql $ordersql LIMIT $limit");

		foreach ($query->result_array() as $value) {
			$list[$value['uid']] = $value;
		}
		$data['designer'] = $list;
		
		cache_write('indexcache', "config['indexcache']", $data);
	}
}

?>